package jgrit.gui;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import jgrit.gui.factory.UIFactory;
import jgrit.options.OptionCollection;

import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class PaletteOptionsPanel extends javax.swing.JPanel {
	/**
	 * Auto-generated serialization version ID
	 */
	private static final long serialVersionUID = 7117726833752912326L;
	
	private JCheckBox paletteCheckBox;
	private JTextField palStartField;
	private JTextField palTransField;
	private JTextField palCountField;

	/**
	* Auto-generated main method to display this 
	* JPanel inside a new JFrame.
	*/
	public static void main(String[] args) {
		JFrame frame = new JFrame();
		frame.getContentPane().add(new PaletteOptionsPanel());
		frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		frame.pack();
		frame.setVisible(true);
	}
	
	@Deprecated
	public PaletteOptionsPanel() {
		this(new OptionCollection());
	}
	
	public PaletteOptionsPanel(OptionCollection options) {
		super();
		initGUI(options);
	}
	
	//TODO: change strings, in this class and others, to use Option.getName()
	private void initGUI(OptionCollection options) {
		try {
			FormLayout thisLayout = new FormLayout(
					"max(p;5dlu), max(p;5dlu):grow, max(p;5dlu), max(p;20dlu):grow, 5dlu", 
					"max(p;5dlu), max(p;5dlu), max(p;5dlu), max(p;5dlu), max(p;5dlu), max(p;5dlu), max(p;5dlu), max(p;5dlu), max(p;5dlu)");
			this.setLayout(thisLayout);
			{
				paletteCheckBox = new JCheckBox();				
				this.add(paletteCheckBox, new CellConstraints("2, 2, 3, 1, default, default"));
				paletteCheckBox.setText(options.paletteEnabled.getName());
							
				UIFactory.attachOption(options.paletteEnabled, paletteCheckBox);
				
				/*
				 * This needs to be a ChangeListener instead of an
				 * ActionListener because the ActionListener won't get fired
				 * if the state change was not caused by the user clicking
				 * on this particular component (i.e. if it was changed on
				 * the task view).
				 * 
				 * I'm not adding until after the attachOption so that the
				 * text fields being controlled have time to get initialized.
				 */
				paletteCheckBox.addChangeListener(new ChangeListener() {
					public void stateChanged(ChangeEvent e) {
						if (paletteCheckBox.isSelected()) {
							palStartField.setEnabled(true);
							palCountField.setEnabled(true);
							palTransField.setEnabled(true);
						} else {
							palStartField.setEnabled(false);
							palCountField.setEnabled(false);
							palTransField.setEnabled(false);
						}
					}
				});
			}
			{
				final JLabel jLabel1 = new JLabel();
				this.add(jLabel1, new CellConstraints("2, 4, 1, 1, default, default"));
				jLabel1.setText("First entry");
			}
			{
				palStartField = new JTextField();
				this.add(palStartField, new CellConstraints("4, 4, 1, 1, default, default"));
				palStartField.setText("0");
				palStartField.setEnabled(paletteCheckBox.isSelected());
				UIFactory.attachOption(options.paletteStart, palStartField);
			}
			{
				final JLabel jLabel2 = new JLabel();
				this.add(jLabel2, new CellConstraints("2, 6, 1, 1, default, default"));
				jLabel2.setText("Number of entries");
			}
			{
				final JLabel jLabel3 = new JLabel();
				this.add(jLabel3, new CellConstraints("2, 8, 1, 1, default, default"));
				jLabel3.setText("Transparent index");
			}
			{
				palCountField = new JTextField();
				this.add(palCountField, new CellConstraints("4, 6, 1, 1, default, default"));
				palCountField.setText("0");
				palCountField.setEnabled(paletteCheckBox.isSelected());
				UIFactory.attachOption(options.paletteCount, palCountField);
			}
			{
				palTransField = new JTextField();
				this.add(palTransField, new CellConstraints("4, 8, 1, 1, default, default"));
				palTransField.setText("0");
				palTransField.setEnabled(paletteCheckBox.isSelected());
				UIFactory.attachOption(options.paletteTrans, palTransField);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
